package com.echo.echovividlive.utils;

import com.coremedia.iso.IsoFile;
import com.echo.echovividlive.common.Constants;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;

public class VideoUtil {
    /**
     * 获取视频文件的播放长度(mp4、mov格式)
     * @param videoPath
     * @return 单位为毫秒
     */
    public static long getMp4Duration(String videoPath) throws IOException {
        IsoFile isoFile = new IsoFile(videoPath);
        long lengthInSeconds =
                isoFile.getMovieBox().getMovieHeaderBox().getDuration() /
                        isoFile.getMovieBox().getMovieHeaderBox().getTimescale();
        return lengthInSeconds;
    }


    /**
     * 得到语音或视频文件时长,单位秒
     * @param filePath
     * @return
     * @throws IOException
     */
    public static long getDuration(String filePath) throws IOException {
        String format = getVideoFormat(filePath);
        long result = 0;
        if("wav".equals(format)){
            result = AudioUtil.getDuration(filePath).intValue();
        }else if("mp3".equals(format)){
            result = AudioUtil.getMp3Duration(filePath).intValue();
        }else if("m4a".equals(format)) {
            result = VideoUtil.getMp4Duration(filePath);
        }else if("mov".equals(format)){
            result = VideoUtil.getMp4Duration(filePath);
        }else if("mp4".equals(format)){
            result = VideoUtil.getMp4Duration(filePath);
        }

        return result;
    }

    /**
     * 得到语音或视频文件时长,单位秒
     * @param filePath
     * @return
     * @throws IOException
     */
    public static long getDuration(String filePath,String format) throws IOException {
        long result = 0;
        if("wav".equals(format)){
            result = AudioUtil.getDuration(filePath).intValue();
        }else if("mp3".equals(format)){
            result = AudioUtil.getMp3Duration(filePath).intValue();
        }else if("m4a".equals(format)) {
            result = VideoUtil.getMp4Duration(filePath);
        }else if("mov".equals(format)){
            result = VideoUtil.getMp4Duration(filePath);
        }else if("mp4".equals(format)){
            result = VideoUtil.getMp4Duration(filePath);
        }

        return result;
    }


    /**
     * 得到文件格式
     * @param path
     * @return
     */
    public static String getVideoFormat(String path){
        return  path.toLowerCase().substring(path.toLowerCase().lastIndexOf(".") + 1);
    }

    /**
     * 判断是否为视频文件
     */
    public static boolean isVideo(MultipartFile file) {
        if (file == null || file.isEmpty()) {
            return false;
        }
        // 获取文件的 MIME 类型
        String contentType = file.getContentType();

        // 判断是否是视频的 MIME 类型
        return contentType != null && contentType.startsWith("video/");
    }

    /**
     * 获取视频封面
     * @param file 视频文件
     * @param outputStream 目标输出流
     * @throws IOException
     */
    public static void getVideoImage(MultipartFile file, OutputStream outputStream) throws IOException {
        long l = System.currentTimeMillis();
        FFmpegFrameGrabber fFmpegFrameGrabber = new FFmpegFrameGrabber(file.getInputStream());
        fFmpegFrameGrabber.start();
        int i = 0;
        int length = fFmpegFrameGrabber.getLengthInFrames();
        Frame frame = null;
        while(i<length){
            frame = fFmpegFrameGrabber.grabImage();
            // 去除前5帧
            if((i>5)&&(frame.image!=null)){
                break;
            }
            i++;
        }
        // 写到目标输出流
        ImageIO.write(FrameToImage(frame), Constants.VIDEO_INDEX_IMAGE_TYPE,outputStream);
        fFmpegFrameGrabber.stop();
        long end = System.currentTimeMillis();
        System.out.println("获取封面总费时:"+(end-l)/1000+"s");
    }


    // 帧转换为图片
    private static BufferedImage FrameToImage(Frame frame){
        Java2DFrameConverter converter = new Java2DFrameConverter();
        BufferedImage image = converter.getBufferedImage(frame);
        return image;
    }
}
